﻿using Abp.Domain.Repositories;
using Abp.UI;
using Mt.Site.Core.AccessStatistics.Entities;
using System;
using System.Collections.Generic;

namespace Mt.Site.Application.AccessStatistics
{
    public class VisitStatisticHelper: MtSiteAppServiceBase,IVisitStatisticHelper
    {
        private readonly IRepository<AccessGeneral> _AccessGeneralRepository;

        private readonly IRepository<AccessData, long> _AccessDataRespository;

        public VisitStatisticHelper(
                IRepository<AccessGeneral> AccessGeneralRepository,
                IRepository<AccessData, long> AccessDataRespository)
        {
            _AccessGeneralRepository = AccessGeneralRepository;
            _AccessDataRespository = AccessDataRespository;
        }

        public string DateToString(DateTime? time)
        {
            try
            {
                var t = (DateTime)time;
                return t.ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {

                return "";
            }

        }

        //数字转为时间 时分秒
        public string NumberToTime(long lengthOfStay,long pvNum,long outNum)
        {
            var number1 = pvNum - outNum;
            if (pvNum == 0 || number1 <= 0)
            {
                return "00:00:00";
            }
            
            var AvgSec = lengthOfStay / 1000 / number1;

            if (AvgSec <= 1)
            {
                return "00:00:00";
            }
            var result = "";
            long hour = AvgSec / 60 / 60;
            long min = AvgSec % 3600 / 60;
            long sec = AvgSec % 60;
            if (hour < 10)
            {
                result += $"0{hour}";
            }
            else
            {
                result += hour.ToString();
            }
            result += ":";
            if (min < 10)
            {
                result += $"0{min}";
            }
            else
            {
                result += min.ToString();
            }
            result += ":";
            if (sec < 10)
            {
                result += $"0{sec}";
            }
            else
            {
                result += sec.ToString();
            }
            return result;
        }

        //跳出率计算
        public double BounceRateCalc(long outNum,long pvNum)
        {
            if (pvNum == 0)
            {
                return 0;
            }
            return Math.Round(outNum * 100.0 / pvNum,2);
        }

        // 评价访问页数
        public double AvgVisitNumCalc(long uvNum,long pvNum)
        {
            if (uvNum == 0)
            {
                return 0;
            }
            return Math.Round(pvNum * 1.0 / uvNum, 2);
        }
        public bool CheckTimeFormat(ref DateTime startTime,ref DateTime endTime)
        {
            var generateEntity = _AccessGeneralRepository.FirstOrDefault(item => item.Id > 0);

            if (generateEntity == null)
            {
                return false;
            }

            if (generateEntity.StartTime != null && generateEntity.StartTime > startTime)
            {
                startTime = (DateTime)generateEntity.StartTime;
            }

            if (endTime > DateTime.Now)
            {
                endTime = DateTime.Now;
            }
            if (startTime > endTime)
            {
                throw new UserFriendlyException(L("ErrorStartTime"));
            }
            if ((endTime - startTime).TotalDays > 356)
            {
                throw new UserFriendlyException(L("InvaildTimeInterval"));
            }
            return true;
        }

        /// <summary>
        /// 确定时间跨度
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<SortTimeType> CheckTimeSpan(DateTime startTime, DateTime endTime)
        {
            TimeSpan span = endTime - startTime;
            var days = span.TotalDays;
            if (days < 1)
            {
                return new List<SortTimeType>
                {
                    SortTimeType.Hour
                };
            }
            else if (days < 7)
            {
                return new List<SortTimeType>
                {
                    SortTimeType.Day
                };
            }
            else if (days < 28)
            {
                return new List<SortTimeType>
                {
                    SortTimeType.Day,
                    SortTimeType.Week
                };
            }
            else
            {
                return new List<SortTimeType>

                {
                    SortTimeType.Day,
                    SortTimeType.Week,
                    SortTimeType.Month
                };
            }

        }

        public string GetVisitSourceName(SourceType source)
        {
            if (source == SourceType.Directaccess)
            {
                return "直接访问";
            }
            else if (source == SourceType.ExternalLinks)
            {
                return "外链访问";
            }
            else if (source == SourceType.SearchEngines)
            {
                return "搜索访问";
            }
            return "";
        }
    }
}
